# -*- coding: utf-8 -*-
import math
import time
import redis
import scrapy

from juqing.items import Actors, Juqings, Plays, Premiere_dates


class JuqingbaSpider(scrapy.Spider):
    """
    拉取首页分类
        :param scrapy.Spider: Spider
    """
    name = 'juqingba'
    allowed_domains = ['www.juqingba.cn']
    start_urls = ['http://www.juqingba.cn/']
    redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    myredis = redis.Redis(connection_pool=redis_pool)

    def parse(self, response):
        """
        docstring here
            :param self: self
            :param response: response object
        """
        classify_titles = list()
        classify_links = list()
        for sel in response.xpath('//div[@class="Nav1"]/ul/li'):
            classify_titles.append(sel.xpath('a/text()').extract_first())
            classify_links.append(sel.xpath('a/@href').extract_first())

        for i, link in enumerate(classify_links):
            if i == 0:
                continue
            else:
                if i == 1:
                    yield scrapy.Request(url=link, callback=self.get_classify)

    def get_classify(self, response):
        """
        获取分类列表
            :param self: self
            :param response: response object
        """
        for sel in response.xpath('//ul[@class="m_Box9 FL"]/li'):
            link = sel.xpath('a/@href').extract_first()
            # 调用获取基本信息 获取任务列表
            # if link == 'http://www.juqingba.cn/dianshiju/62036.html':
            yield scrapy.Request(url=link, callback=self.get_base)

        next_page = response.xpath(
            '//ul[@class="pagelist FR"]/li[last()-3]/a/@href').extract_first()
        url = response.urljoin(next_page)
        yield scrapy.Request(url=url, callback=self.get_classify)

    def get_base(self, response):
        """
        获取剧情基本信息
            :param self: self
            :param response: response object
        """
        base = response.xpath('//div[@class="txt"]')
        item = Plays()
        item['title'] = response.xpath(
            '//div[@class="m_T3 FL"]/h1/text()').extract_first()
        item['poster'] = response.xpath(
            '//div[@class="pic FL"]/img/@src').extract_first()
        item['protagonist'] = base.xpath('p[1]/text()').extract_first()
        item['play_set_number'] = base.xpath(
            'p[2]/span[1]/text()').extract_first()
        item['director'] = base.xpath('p[2]/span[2]/text()').extract_first()
        item['status'] = base.xpath('p[3]/span[1]/text()').extract_first()
        item['scriptwriter'] = base.xpath(
            'p[3]/span[2]/text()').extract_first()
        item['year'] = base.xpath('p[4]/span[1]/text()').extract_first()
        item['area'] = base.xpath('p[4]/span[2]/text()').extract_first()
        item['premiere_date'] = base.xpath(
            'p[5]/span[1]/text()').extract_first()
        item['premiere_platform'] = base.xpath(
            'p[5]/span[2]/text()').extract_first()
        item['play_type'] = base.xpath('p[6]/text()').extract_first()
        item['intro'] = response.xpath('//div[@class="m_jq"]/p').extract()
        item['create_at'] = math.ceil(time.time())
        yield item
        titles = ['剧情介绍', '分集剧情', '演员表', '人物关系图', '播出时间']
        for sel in response.xpath('//ul[@class="Sub"]/ul/li'):
            sub = dict()
            sub['title'] = sel.xpath('a/text()').extract_first()
            sub['link'] = sel.xpath('a/@href').extract_first()
            if sub['title'] == '分集剧情':
                yield scrapy.Request(url=sub['link'], callback=self.get_detail)
            if sub['title'] == '演员表':
                yield scrapy.Request(url=sub['link'], callback=self.get_actors)
            if sub['title'] == '播出时间':
                yield scrapy.Request(url=sub['link'], callback=self.get_premiere_date)

    def get_actors(self, response):
        """
        获取演员信息 可选
            :param self: self
            :param response: response object
        """
        item = Actors()
        item['title'] = response.xpath(
            '//div[@class="m_T3 FL"]/h1/text()').extract_first()
        item['content'] = response.xpath(
            '//div[@class="m_jq"]/p').extract()
        yield item

    def get_detail(self, response):
        """
        获取剧情剧集详细 可选
            :param self: self
            :param response: response object
        """
        item = Juqings()
        item['title'] = response.xpath(
            '//div[@class="m_jq"]/h2/text()').extract_first()
        item['content'] = response.xpath(
            '//div[@class="m_jq"]/p').extract()
        yield item
        sel = response.xpath(
            '//div[@class="m_Box15"][1]/a[@class="thisclass"]/following-sibling::a[1]')
        link = sel.xpath('@href').extract_first()
        url = response.urljoin(link)
        yield scrapy.Request(url=url, callback=self.get_detail)

    def get_premiere_date(self, response):
        """
        获取剧情播出时间 可选
            :param self: self
            :param response: response object
        """
        item = Premiere_dates()
        item['title'] = response.xpath(
            '//div[@class="m_T3 FL"]/h1/text()').extract_first()
        item['content'] = response.xpath(
            '//div[@class="m_jq"]/p').extract()
        yield item
